package com.wzh.user.biz.sysUser.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzh.common.general.result.PageQuery;
import com.wzh.common.general.result.PageResult;
import com.wzh.common.general.result.Result;
import com.wzh.common.mybatis.util.MybatisPageUtils;
import com.wzh.user.api.dto.SysUserDTO;
import com.wzh.user.api.feign.RemoteUserFeignClient;
import com.wzh.user.biz.sysUser.domain.entity.SysUser;
import com.wzh.user.biz.sysUser.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统用户管理控制器，提供用户的增删改查等操作接口
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-15-19:42
 */
@Tag(name = "系统用户表管理")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController implements RemoteUserFeignClient {

    private final SysUserService sysUserService;

    @Operation(summary = "分页查询系统用户表列表")
    @PostMapping("/page")
    public Result<PageResult<SysUserDTO>> page(@RequestBody PageQuery<SysUserDTO> pageQuery) {
        return Result.success(sysUserService.page(pageQuery));
    }

    @Operation(summary = "查询系统用户表列表")
    @GetMapping
    public Result<List<SysUserDTO>> list() {
        return Result.success(MybatisPageUtils.convertList(sysUserService.list(), SysUserDTO.class));
    }

    @Operation(summary = "根据ID查询系统用户表")
    @GetMapping("/{id}")
    public Result<SysUserDTO> getById(@PathVariable Long id) {
        return Result.success(MybatisPageUtils.convert(sysUserService.getById(id), SysUserDTO.class));
    }

    @Operation(summary = "新增或修改系统用户表")
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody SysUserDTO dto) {
        return Result.success(sysUserService.saveOrUpdate(MybatisPageUtils.convert(dto, SysUser.class)));
    }

    @Operation(summary = "新增系统用户表")
    @PostMapping
    public Result<Boolean> save(@RequestBody SysUserDTO dto) {
        return Result.success(sysUserService.save(MybatisPageUtils.convert(dto, SysUser.class)));
    }

    @Operation(summary = "修改系统用户表")
    @PutMapping
    public Result<Boolean> update(@RequestBody SysUserDTO dto) {
        return Result.success(sysUserService.updateById(MybatisPageUtils.convert(dto, SysUser.class)));
    }

    @Operation(summary = "删除系统用户表")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(sysUserService.removeById(id));
    }

    @Operation(summary = "检查重复用户")
    @PostMapping("/checkUserDuplicate")
    public Result<Boolean> checkUserDuplicate(@RequestBody SysUserDTO dto) {
        return Result.success(this.sysUserService.checkUserDuplicate(dto));
    }

    @Override
    @SaIgnore
    public Result<SysUserDTO> getOne(SysUserDTO userDTO) {
        SysUser user = this.sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(CharSequenceUtil.isNotBlank(userDTO.getUsername()), SysUser::getUsername, userDTO.getUsername())
                .eq(ObjectUtil.isNotNull(userDTO.getId()), SysUser::getId, userDTO.getId()));
        if (ObjectUtil.isNull(user)) {
            return Result.error("用户不存在");
        }
        return Result.success(MybatisPageUtils.convert(user, SysUserDTO.class));
    }

    @Override
    public Result<Boolean> updateById(SysUserDTO userDTO) {
        return Result.success(this.sysUserService.updateById(MybatisPageUtils.convert(userDTO, SysUser.class)));
    }
}